﻿namespace h6

{

    public class Animal

    {

        public virtual void Voice()

        {

        }

    }



    public class Cat : Animal

    {

        public override void Voice()

        {

            Console.WriteLine("猫：喵喵");

        }

    }



    public class Pig : Animal

    {

        public override void Voice()

        {

            Console.WriteLine("猪：哼哼");

        }

    }



    public class Dog : Animal

    {

        public override void Voice()

        {

            Console.WriteLine("狗：汪汪");

        }

    }



    public class Store

    {

        public Animal GetInstance(string animalType)

        {

            string type = animalType.ToLower();



            if (type == "dog")

            {

                return new Dog();

            }

            else if (type == "pig")

            {

                return new Pig();

            }

            else

            {

                return new Cat();

            }

        }

    }



    internal class Program

    {

        public static void Main(string[] args)

        {

            Store petStore = new Store();



            Animal dog = petStore.GetInstance("dog");

            dog.Voice();



            Animal pig = petStore.GetInstance("pig");

            pig.Voice();



            Animal cat1 = petStore.GetInstance("cat");

            cat1.Voice();

            // 测试传入无效参数时返回猫对象

            Animal cat2 = petStore.GetInstance("bird");

            cat2.Voice();

        }

    }

}

